
import copy

from coffeetalk.protocol import ioelement,ioml

class Update(object):
    
    def __init__(self,env,path):
        self.env = env
        self.path = path
    
class Add(Update):
    
    def __init__(self,env,path,newioelement):

        Update.__init__(self,env,path)
        
        self.header = ioelement.IOElement('Update',{'type':'updates.Add','path':self.path})
        self.header.add(env.element)
        
        self.bodyelement = copy.deepcopy(newioelement)

    def __str__(self):
        return str(ioml.IOML(headelement=self.header,bodyelement=self.bodyelement))

class AnchorAdd(Update):
    
    def __init__(self,env,anchorpath,path,newioelement):
        
        Update.__init__(self, env, path)
        
        self.header = ioelement.IOElement('AnchorUpdate',{'type':'updates.AnchorAdd','anchorpath':anchorpath,'path':self.path})
        self.header.add(env.element)
        
        self.bodyelement = copy.deepcopy(newioelement)
        
    def __str__(self):
        return str(ioml.IOML(headelement=self.header,bodyelement=self.bodyelement))
       
class Delete(Update):
    
    def __init__(self,env,path,key):

        Update.__init__(self,env,path)
        
        self.header = ioelement.IOElement('Update',{'type':'updates.Delete','path':self.path,'key':key})
        self.header.add(env.element)
    
    def __str__(self):
        return str(ioml.IOML(headelement=self.header))

class AnchorDelete(Update):
    
    def __init__(self,env,anchorpath,path,key):
        
        Update.__init__(self, env, path)
        
        self.header = ioelement.IOElement('AnchorUpdate',{'type':'updates.AnchorDelete','anchorpath':anchorpath,'path':self.path,'key':key})
        self.header.add(env.element)
                
    def __str__(self):
        return str(ioml.IOML(headelement=self.header))
 
class Remove(Update):
    
    def __init__(self,env,path):
        
        Update.__init__(self,env,path)
        
        self.header = ioelement.IOElement('Update',{'type':'updates.Remove','path':self.path})
        self.header.add(env.element)
        
    def __str__(self):
        return str(ioml.IOML(headelement=self.header))
    
class Redirect(Update):
    
    def __init__(self,env,path,newpath):
        
        Update.__init__(self,env,path)
        
        self.newpath = newpath
        
        self.header = ioelement.IOElement('Update',{'type':'updates.Redirect','path':self.path,'newpath':self.newpath})

    def __str__(self):
        return str(ioml.IOML(headelement=self.header))
        